package com.frogs.ucenter.service.impl;

import java.util.Arrays;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.entity.UserFamilyMember;
import com.frogs.ucenter.service.AbsService;
import com.frogs.ucenter.service.UserFamilyMemberService;

@Service("userFamilyMemberServiceImpl")
public class UserFamilyMemberServiceImpl extends AbsService implements UserFamilyMemberService {
	private static Logger log = LoggerFactory.getLogger(UserFamilyMemberServiceImpl.class);
	
	@Override
	public JSONObject getFamilyMemberInfo(Integer familyMemberId,Integer userId) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_FAMILYMEMBER_INFO");
		String[] otherParams = {
					ApiUrlParamEnum.familyMemberId.getParamName(),familyMemberId.toString(),
					ApiUrlParamEnum.userId.getParamName(),userId.toString(),
					
				};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
			return result.getJSONObject("response").getJSONObject("info");
		} catch (Exception e) {
			log.error("func[getFamilyMemberInfo] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return null;
	}

	@Override
	public JSONObject getFamilyMemberList(Integer userId) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_FAMILYMEMBER_LIST");
		String[] otherParams = {
				ApiUrlParamEnum.userId.getParamName(),userId.toString(),
				};
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (null == result || !result.containsKey("response") 
					|| ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (Exception e) {
			log.error("func[getFamilyMemberList] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}

	@Override
	public JSONObject addFamilyMember(UserFamilyMember userFamilyMember, String ip) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_FAMILYMEMBER_ADD");
		
		String[] otherParams = {
				ApiUrlParamEnum.userId.getParamName(),userFamilyMember.getUserId().toString(),
				ApiUrlParamEnum.relatives.getParamName(),userFamilyMember.getRelatives(),
				ApiUrlParamEnum.sex.getParamName(),userFamilyMember.getSex().toString(),
				ApiUrlParamEnum.diseaseTag.getParamName(),userFamilyMember.getDiseaseTag(),
				ApiUrlParamEnum.age.getParamName(),userFamilyMember.getAge().toString(),
				ApiUrlParamEnum.ip.getParamName(), ip,
				};
		
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (Exception e) {
			log.error("func[addFamilyMember] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}

	@Override
	public JSONObject deleteFamilyMember(Integer familyMemberId,Integer userId) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_FAMILYMEMBER_DELETE");
		
		String[] otherParams = {
					ApiUrlParamEnum.userId.getParamName(),userId.toString(),
					ApiUrlParamEnum.familyMemberId.getParamName(),familyMemberId.toString(),
					
				};
		
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (Exception e) {
			log.error("func[deleteFamilyMember] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}

	@Override
	public JSONObject updateFamilyMember(UserFamilyMember userFamilyMember) {
		ApiUrl apiUrl = this.buildUcenterApiBaseUrl("USER_FAMILYMEMBER_UPDATE");
		
		String[] allParams = null;
		
		
		String[] otherParams = {
				ApiUrlParamEnum.familyMemberId.getParamName(),userFamilyMember.getId().toString(),
				ApiUrlParamEnum.userId.getParamName(),userFamilyMember.getUserId().toString(),
				ApiUrlParamEnum.relatives.getParamName(),userFamilyMember.getRelatives(),
				ApiUrlParamEnum.sex.getParamName(),userFamilyMember.getSex().toString(),
				ApiUrlParamEnum.diseaseTag.getParamName(),userFamilyMember.getDiseaseTag(),
				ApiUrlParamEnum.age.getParamName(),userFamilyMember.getAge().toString(),
				};
		String[] otherParams1 = {
				ApiUrlParamEnum.familyMemberId.getParamName(),userFamilyMember.getId().toString(),
				ApiUrlParamEnum.userId.getParamName(),userFamilyMember.getUserId().toString(),
				ApiUrlParamEnum.diseaseTag.getParamName(),userFamilyMember.getDiseaseTag(),
				};
		
		String relatives = userFamilyMember.getRelatives();
		if(null == relatives || "".equals(relatives)){
			allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams1);
		}else{
			allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		}
		
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (Exception e) {
			log.error("func[updateFamilyMember] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),Arrays.deepToString(allParams), e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}

}
